﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.Models;
using RMS.Models.DatabaseManager;
using System.Web.ModelBinding;
using System.Data.Entity.Infrastructure;
using RMS.Models.Repository;

namespace RMS.Pages.Admin
{
    public partial class Users : System.Web.UI.Page
    {

        private UserManager userManager = new UserManager();
        private Repository repo = new Repository();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public IEnumerable<User> GetUsers()
        {
            string comboboxSelection = cbUserSelect.Value;

            if (comboboxSelection.Equals("All Users"))
            {
                return userManager.AllUsers.OrderBy(p => p.Us_LName);
            }
            else if (comboboxSelection.Equals("Basic Users"))
            {
                return userManager.AllUsers.OrderBy(p => p.Us_LName)
                    .Where(p => p.Us_IsAdmin == false);
            }
            else // Must be Admin User
            {
                return userManager.AllUsers.OrderBy(p => p.Us_LName)
                    .Where(p => p.Us_IsAdmin == true);
            }

        }

        public void UpdateUser(int Us_Id)
        {
            User user = userManager.AllUsers.Where(u => u.Us_ID == Us_Id).FirstOrDefault();
            string password = user.Us_Password;
                

            IValueProvider provider = new FormValueProvider(ModelBindingExecutionContext);

            if (ModelState.IsValid)
            {
                if (user != null && TryUpdateModel<User>(user, provider))
                {
                    if (user.Us_Password == null)
                        user.Us_Password = password;
                    userManager.UpdateUser(user);
                }
            }
            
        }

        public void DeleteUser(int Us_Id)
        {
                                   
            // Delete Volunteer Items First...
            var volunteerItems = repo.VolunteerItems;
                        
            foreach (VolunteerItem v in volunteerItems)
            {
                if (v.User_Us_ID == Us_Id)
                {
                    repo.DeleteVolunteerItem(v);
                }
            }

            User user = userManager.GetUser(Us_Id);
            userManager.DeleteUser(user);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ListView1.DataBind();
        }

        // For setting up true/false comboboxes
        public string returnOpposite(Boolean str)
        {

            if (str == false)
            {
                return "True";
            }
            else
                return "False";
        }

        // Method for getting volunteering submissions
        public string getVolunteerSubmissions(int userID)
        {
            string str = "";
            var volunteerItems = repo.VolunteerItems;
            int count = 0;

            // Loop cycles through Volunteer Items
            foreach (VolunteerItem v in volunteerItems)
            {

                if (v.User_Us_ID == userID)
                {
                    if (count == 0)
                    {
                        str += v.VolunteerItemType;
                    }
                    else
                    {
                        str += ", " + v.VolunteerItemType;
                    }
                    count++;
                }
            }

            if (str.Equals(""))
            {
                str += "No volunteering selections found.";
            }

            return str;
        }
    }
}